<?php
/**
 * Created by PhpStorm.
 * User: luxiao
 * Date: 2017/5/8
 * Time: 16:49
 */
namespace app\index\controller;


use think\Controller;

class ExcelController extends Controller
{
    function excel()
    {
        $path = dirname(__FILE__); //找到当前脚本所在路径
        vendor("PHPExcelClass.PHPExcel");　//手动引入PHPExcel.php
        import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');　　//引入IOFactory.php 文件里面的PHPExcel_IOFactory这个类
        $PHPExcel = new \PHPExcel();　　//实例化
        $PHPSheet = $PHPExcel->getActiveSheet();　
        $PHPSheet->setTitle("demo"); //给当前活动sheet设置名称
        $PHPSheet->setCellValue("A1","姓名")->setCellValue("B1","分数");//表格数据
        $PHPSheet->setCellValue("A2","张三")->setCellValue("B2","2121");//表格数据
        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,"Excel2007");　　//创建生成的格式
        header('Content-Disposition: attachment;filename="表单数据.xlsx"');　　//下载下来的表格名
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
    }
}